//
//  ST_TIANRichTextStorageComponent.swift
//  btvideo
//
//  Created by Jason on 2021/11/2.
//  Copyright © 2021 JarvanZhang. All rights reserved.
//

import Foundation
import RxCocoa
import RxSwift
import SwiftyJSON
import NSObject_Rx

class ST_TIANRichTextStorageComponent:NSObject{
    
    private var maleJson = JSON.null
    private var femaleJson = JSON.null

    /// 重试延时 1 秒
    private let retry_delay: RxTimeInterval = RxTimeInterval.seconds(1)
    
    
    
    /// 一级页面刷新使用
    let pulled_observable = PublishSubject<Void>()
    
    var json:JSON{
        
        if ST_BTUserInfoManager.shared.current_account_info?.userinfo?.sex == .male{
            if maleJson.type == .null{
                pull_config(type: 1)
            }
            return maleJson
        }else{
            if femaleJson.type == .null{
                pull_config(type: 2)
            }
            return femaleJson
        }
    }
    
}

extension ST_TIANRichTextStorageComponent{
    
    fileprivate func pull_config(type:Int){
        
        TIANSession(requestUrl: PH.tainBigHeartApi, parameters: ["type":type]).wrap_response_data().retry { error -> Observable<Int> in
            return Observable.timer(self.retry_delay, scheduler: MainScheduler.instance)
        }.subscribe(onNext: { json in
            
            if type == 1{
                self.maleJson = json["data"]
            }else{
                self.femaleJson = json["data"]
            }
            
            self.pulled_observable.onNext(())
            
        }).disposed(by: rx.disposeBag)
        
    }
}
